﻿// -----------------------------------------------------------------------
// <copyright file="HRDepartment.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace SoftServe.HrManager
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using SoftServe.HrManager.Abstractions;
    using SoftServe.HrManager.Models;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class HrDepartment
    {
        private HrManager mHrManager = new HrManager();
        private HrResources mHrResources = new HrResources();
        public int MaxCandidates { get; set; }
        Queue<BackEndDeveloperBase> mBackEndDevelopers = default(Queue<BackEndDeveloperBase>);
        Queue<FrontEndDeveloperBase> mFrontEndDevelopers = default(Queue<FrontEndDeveloperBase>);

        public HrDepartment()
        {
            MaxCandidates = 255;
            StartCastingCandidates();
            HireCandidates();
        }

        private void HireCandidates()
        {
            var forBack = new List<Candidate>(mHrResources.GetCandidatesBySkills(mHrManager.BackEndSkills));
            mBackEndDevelopers = new Queue<BackEndDeveloperBase>(mHrManager.SelectBackEndCandidates(forBack));

            var forFront = new List<Candidate>(mHrResources.GetCandidatesBySkills(mHrManager.FrontEndSkills));
            mFrontEndDevelopers = new Queue<FrontEndDeveloperBase>(mHrManager.SelectFrontEndCandidates(forFront));
        }


        public IEnumerable<Team> FormCompanyDeveloperForce <T> (int backDevsCount, int frontDevsCount) where T : IDeveloper
        {
            int minlength = Math.Min(mFrontEndDevelopers.Count, mBackEndDevelopers.Count);
            List<T> list1 = new List<T>(mFrontEndDevelopers.OfType<T>());
            List<T> list2 = new List<T>(mBackEndDevelopers.OfType<T>());

            Queue<T> TFrontEndDevelopers = new Queue<T>(list1);
            Queue<T> TBackEndDevelopers = new Queue<T>(list2);
            int i = 0;
            while (minlength > frontDevsCount && minlength > backDevsCount)
            {
                i++;
                Team t = new Team();
                for (int j = 0; j < frontDevsCount; j++)
                {
                    t.Add(TFrontEndDevelopers.Dequeue() as IDeveloper);
                }
                for (int j = 0; j < backDevsCount; j++)
                {
                    t.Add(TBackEndDevelopers.Dequeue() as IDeveloper);
                }
                yield return t;
                minlength = Math.Min(TFrontEndDevelopers.Count, TBackEndDevelopers.Count);
            }
        }

        public void StartCastingCandidates()
        {
            Random random = new Random();
            for (int i = 0; i < MaxCandidates; i++)
			{
                mHrResources.Add(new Candidate()
                {
                    Name = string.Format("Gary {0}", i),
                    BDate = new DateTime(1977,02,23),
                    Phone = string.Format("+380{0}{1}{2}{3}{5}", i, i+1, i, i+2, i+1, i),
                    Skills = new List<eSkillName>()
                    {
                        (eSkillName)random.Next(0, 5),
                        (eSkillName)random.Next(0, 5),
                        (eSkillName)random.Next(0, 5)
                    }
                });
			}



        }
    }
}
